package drds.binlog.event;

import drds.binlog.LogByteBlock;
import drds.binlog.LogEvent;

/**
 * Append_block_log_event.
 *
 * @author <a href="mailto:changyuan.lh@taobao.com">Changyuan.lh</a>
 * @version 1.0
 */
public class AppendBlockLogEvent extends LogEvent
{

    /* AB = "Append Block" */
    public static final int AB_FILE_ID_OFFSET = 0;
    private final LogByteBlock blockBuf;
    private final int blockLen;
    private final long fileId;

    public AppendBlockLogEvent(LogHeader logHeader, LogByteBlock logByteBlock, FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        super(logHeader);

        final int commonHeaderLength = formatDescriptionLogEvent.commonHeaderLength;
        final int postHeaderLength = formatDescriptionLogEvent.postHeaderLengths[logHeader.type - 1];
        final int totalHeaderLen = commonHeaderLength + postHeaderLength;

        logByteBlock.index(commonHeaderLength + AB_FILE_ID_OFFSET);
        fileId = logByteBlock.getUint32BasedOnIndex();

        logByteBlock.index(postHeaderLength);
        blockLen = logByteBlock.limit() - totalHeaderLen;
        blockBuf = logByteBlock.duplicateBasedOnIndex(blockLen);
    }

    public final long getFileId()
    {
        return fileId;
    }

    public final LogByteBlock getBuffer()
    {
        return blockBuf;
    }

    public final byte[] getData()
    {
        return blockBuf.getData();
    }
}
